package com.pr.makfilm.user.pages.room.list;

import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.DataProvider;
import com.pr.makfilm.core.cmp.table.SelectionChangeCallback;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.domain.filter.RoomFilter;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.user.api.UserApiMappings;
import com.pr.makfilm.user.pages.AbstractUserPage;
import com.pr.makfilm.user.pages.room.list.cmp.RoomFilterToolbar;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 27.09.13
 * Time: 00:28
 * To change this template use File | Settings | File Templates.
 */
@MountPath(UserApiMappings.ROOM_LIST_PAGE)
@AuthorizeInstantiation(UserAuthorities.USER)
public class RoomListPage extends AbstractUserPage
{
   @SpringBean
   private IRoomService roomService;

   @SpringBean
   private ISecurityService securityService;

   private RoomFilterToolbar filterToolbar;

   private AjaxDataTable<Room> table;

//   private Room selected;

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      List<IColumn<Room, String>> columns = new ArrayList<IColumn<Room, String>>();
      columns.add(new PropertyColumn<Room, String>(
              new ResourceModel("name"),
              Room.FIELD_NAME,
              Room.FIELD_NAME
      ));
      columns.add(new PropertyColumn<Room, String>(
              new ResourceModel("city"),
              Room.FIELD_CITY,
              Room.FIELD_CITY
      ));
      columns.add(new PropertyColumn<Room, String>(
              new ResourceModel("street"),
              Room.FIELD_STREET,
              Room.FIELD_STREET
      ));
      columns.add(new PropertyColumn<Room, String>(
              new ResourceModel("zip"),
              Room.FIELD_ZIP,
              Room.FIELD_ZIP
      ));
      columns.add(new PropertyColumn<Room, String>(
              new ResourceModel("capacity"),
              Room.FIELD_CAPACITY,
              Room.FIELD_CAPACITY
      ));
      columns.add(new PropertyColumn<Room, String>(
              new ResourceModel("lease.price"),
              Room.FIELD_LEASE_PRICE,
              Room.FIELD_LEASE_PRICE
      ));

      OpenSearchDescription<Room> osd = new OpenSearchDescription<Room>();
      RoomFilter filter = new RoomFilter();
      filter.setObjectState(ObjectStateEnum.ACTIVE);
      filter.setUser(securityService.getLoggedInUser());
      osd.setFilter(filter);
      DataProvider<Room> dataProvider = new DataProvider<Room>(roomService, osd);
      table = new AjaxDataTable<Room>("table", columns, dataProvider);
      table.addFilterToolbar(filterToolbar = new RoomFilterToolbar(table, new PropertyModel<Room>(table, "selected")));
      table.setSelectionChangeCallback(new SelectionChangeCallback<Room>()
      {
         @Override
         public void onSelectionChange(AjaxRequestTarget target, IModel<Room> model)
         {
            refreshTableView(target);
         }
      });
      add(table);
   }

   private void refreshTableView(AjaxRequestTarget target)

   {
      target.add(table);
      target.add(filterToolbar);
   }
}
